package xju.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import xju.ssm.entity.User;
import xju.ssm.service.UserService;

import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    //从IOC容器中找到匹配的实例注入进来.
    @Autowired
    UserService userService;

    @RequestMapping("/list")
    public ModelAndView queryAll(ModelAndView modelAndView){
        List<User> list = userService.queryAll();
        modelAndView.addObject("userList",list);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/add")
    public String add(User user){
        int i = userService.add(user);
        return i!=0?"success":"fail";
    }

    @RequestMapping("/delete")
    public ModelAndView delete(int id,ModelAndView mod){
        int i =userService.delete(id);
        ModelAndView mav= queryAll(mod);
        return mav;
    }

    @RequestMapping("/update")
    public ModelAndView update(User user,ModelAndView mod){
        int i = userService.update(user);
        ModelAndView mav= queryAll(mod);
        return mav;
    }









    /*@RequestMapping("/hello")
    public void test01(){
//        userService = new UserSeviceImpl();
        System.out.println("Hello World"+userService.queryAll());
    }*/
}
